using IdentityManagerServiceApi.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SharedClassLibrary.Contracts;
using SharedClassLibrary.DTOs;

namespace IdentityManagerServiceApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        private readonly AppDbContext dbContext;
        private readonly IUserService userService;

        public HomeController(AppDbContext dbContext, IUserService userService)
        {
            this.dbContext = dbContext;
            this.userService = userService;
        }

        [HttpGet]
        [Authorize(Roles = "admin")]
        public IActionResult Get()
        {
            var list = dbContext.Users.ToList();
            return Ok(list);
        }

        [HttpPost]
        [Route("/login")]
        public IActionResult Login(LoginDTO model)
        {
            var response = userService.LoginAsync(model);
            return Ok(response.Result);
        }

        [HttpPost]
        [Route("/create")]
        public IActionResult Create(UserDTO userDTO)
        {
            var response = userService.CreateAccountAsync(userDTO);
            return Ok(response.Result);
        }
    }
}
